package com.liao.seckill.controller;

import com.liao.seckill.pojo.*;
import com.liao.seckill.rabbitmq.MiaoShaMessage;
import com.liao.seckill.rabbitmq.MiaoShaSender;
import com.liao.seckill.result.Result;
import com.liao.seckill.result.ResultCode;
import com.liao.seckill.service.GoodsMiaoshaService;
import com.liao.seckill.service.GoodsService;
import com.liao.seckill.service.OrderService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("/goods")
public class GoodsController {

    private final GoodsMiaoshaService goodsMiaoshaService;
    private final OrderService orderService;
    private final MiaoShaSender miaoShaSender;

    public GoodsController(GoodsMiaoshaService goodsMiaoshaService, OrderService orderService,
                           MiaoShaSender miaoShaSender) {
        this.goodsMiaoshaService = goodsMiaoshaService;
        this.orderService = orderService;
        this.miaoShaSender = miaoShaSender;
    }

    @ResponseBody
    @RequestMapping(value = "/miaosha/list", method = RequestMethod.GET)
    public Result<Object> findMiaoShaGoods() {
        List<TbGoodsMiaoshaAndGoods> miaoshaGoods = goodsMiaoshaService.findMiaoshaGoods();
        return new Result<>(miaoshaGoods);
    }

    /**
     * @param request
     * @param id      秒杀商品的Id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/secKill", method = RequestMethod.POST)
    public Result<Object> secKill(HttpServletRequest request, int id) {
        Integer userId = (Integer) request.getAttribute("userId");
//        TbUser tbUser = new TbUser();
//        int i = new Random().nextInt();
//        tbUser.setId(i);
        TbOrderInf tbOrderInf = orderService.findOrderByUserIdAndGoodsId(userId, id);
        if (tbOrderInf != null) {
            return new Result<>(tbOrderInf, ResultCode.ORDER_CREATED);
        }
        TbGoodsMiaosha goodsMiaosha = goodsMiaoshaService.findById(id);
        if (goodsMiaosha == null) {
            return new Result<>(ResultCode.GOODS_NOTFOUND);
        }
        tbOrderInf = orderService.secKill(goodsMiaosha, userId);
        if (tbOrderInf == null) {
            return new Result<>(ResultCode.ORDER_CREATE_ERROR);
        }
        return new Result<>(tbOrderInf, ResultCode.SUCCESS);
    }

    /**
     * 加入消息队列
     * 把查询之类的语句全部放在发送消息之后执行，又能快不少
     * 什么都不加吞吐量2400,加上sendMiaoShaMessage之后1700,然后把sendMiaoShaMessage变成异步方法变2100
     *
     * @param request
     * @param id      秒杀商品的Id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/secKill_mq", method = RequestMethod.POST)
    public Result<Object> secKillMQ(HttpServletRequest request, int id) {
        Integer userId = (Integer) request.getAttribute("userId");
//        TbUser tbUser = new TbUser();
//        int i = new Random().nextInt();
//        tbUser.setId(i);
        miaoShaSender.sendMiaoShaMessage(new MiaoShaMessage(userId, id));
        return new Result<>(ResultCode.SUCCESS);
    }

    @ResponseBody
    @RequestMapping(value = "/result", method = RequestMethod.GET)
    public Result<Object> miaoShaResult(HttpServletRequest request, Integer goodsId) {
        if (goodsId == null) {
            return new Result<>(ResultCode.PARAMETER_ERROR);
        }
        Integer userId = (Integer) request.getAttribute("userId");
        TbOrderInf tbOrderInf = orderService.findOrderByUserIdAndGoodsId(userId, goodsId);
        if (tbOrderInf == null) {
            return new Result<>(ResultCode.GOODS_MIAOSHA_FILE);
        }
        return new Result<>(tbOrderInf, ResultCode.SUCCESS);
    }
}
